package net.lz1998.pbbot.plugin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotPlugin;
import net.lz1998.pbbot.constant.BannedConstant;
import net.lz1998.pbbot.utils.HttpRequestUtils;
import net.lz1998.pbbot.utils.Msg;
import onebot.OnebotEvent;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * @ProjectName spring-mirai-server
 * @Description:
 * @Author WT
 * @CreateDate 2020/12/29 13:17
 */

//自动回复
@Component
@Slf4j
@RequiredArgsConstructor
public class SmallTalkPlugin extends BotPlugin {

	private static boolean flag = false;

	private static boolean flag1 = true;

	private static final String QQ="<at qq=\"1481062763\"";

	private final HelloPlugin helloPlugin;


	@SneakyThrows
	@Override
	public int onGroupMessage(@NotNull Bot bot, @NotNull OnebotEvent.GroupMessageEvent event) {
		String msg = event.getRawMessage().trim();
		long groupId = event.getGroupId();
		long id = event.getUserId();
		if(BannedConstant.ADMINID == id||BannedConstant.ADMINID1 == id){
			if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.LIAOTIANON)) {
				flag = true;
				bot.sendGroupMsg(groupId, "群聊模式开启", true);
				bot.sendGroupMsg(groupId, Msg.builder().face(1), false);
			} else if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.LIAOTIANOFF)) {
				flag = false;
				bot.sendGroupMsg(groupId, "群聊模式关闭", false);
				bot.sendGroupMsg(groupId, Msg.builder().face(0), false);
			}

			if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.SILIAOON)) {
				flag1 = true;
				bot.sendGroupMsg(groupId, "私聊模式开启", true);
				bot.sendGroupMsg(groupId, Msg.builder().face(1), false);
			} else if (BannedConstant.GROUPID == groupId && msg.startsWith(BannedConstant.SILIAOOFF)) {
				flag1 = false;
				bot.sendGroupMsg(groupId, "私聊模式关闭", false);
				bot.sendGroupMsg(groupId, Msg.builder().face(0), false);
			}
		}


		if(!flag) {
			System.out.println("群聊模式关闭中...群聊跳过");
			return MESSAGE_IGNORE;
		}

		if (BannedConstant.GROUPID == groupId
				|| BannedConstant.GROUPID1 == groupId
				|| BannedConstant.GROUPID2 == groupId
				|| BannedConstant.GROUPID3 == groupId
				|| BannedConstant.GROUPID4 == groupId) {

			System.out.println("消息内容:" + msg);

			if (msg.contains("qq=")) {
				msg = msg.replaceAll(Pattern.compile("[^0-9]").matcher(msg).replaceAll(""), "");
				if (msg.length() == 11) {
					System.out.println("跳过不回复了");
					return MESSAGE_IGNORE;
				}
			}

			if (msg.contains("qq=")) {
				msg = msg.replaceAll(Pattern.compile("[^0-9]").matcher(msg).replaceAll(""), "");
				if (msg.length() != 11) {
					msg = msg.replaceAll("<at qq=\"\"/>", "").trim();
				}
			}

			//判断name是否包含emoji表情
			if (msg.contains("<face")) {
				String number = Pattern.compile("[^0-9]").matcher(msg).replaceAll("");
				helloPlugin.messageGroupFacePush(groupId,bot,id,number);
				return MESSAGE_IGNORE;
			}

			if (msg.trim().contains("image")) {
				String url = msg.substring(msg.indexOf("url=") + 5, msg.length() - 3);
				Msg image = Msg.builder().at(id).image(url);
				helloPlugin.messageGroupImagePush(bot,groupId,image);
				return MESSAGE_IGNORE;
			} else {
				String s = "";
				int i = 0;
				do {
					try {
						s = HttpRequestUtils.doGet("https://api.qingyunke.com/api.php?key=free&appid=0&msg=" + URLEncoder.encode(msg.trim(), "UTF-8"));
						i++;
					} catch (Exception e) {
						bot.sendGroupMsg(groupId, Msg.builder().at(id).text(BannedConstant.WENTI), false);
					}
				} while (StringUtils.isBlank(s) && i < 10);
				JSONObject jsonObject = JSON.parseObject(s);
				if(jsonObject.getString("result").equals("0")){
					s=jsonObject.getString("content");
				}
				System.out.println("收到用户:" + event.getUserId() + "消息,内容为:" + msg);
				System.out.println("回复内容:" + s);
				helloPlugin.messageGroupPush(s,bot,groupId,id);
			}
		}
		return MESSAGE_IGNORE;
	}

	@SneakyThrows
	@Override
	public int onPrivateMessage(@NotNull Bot bot, @NotNull OnebotEvent.PrivateMessageEvent event) {

		if(!flag1) {
			System.out.println("私聊模式关闭中...私聊跳过");
			return MESSAGE_IGNORE;
		}
		String msg = event.getRawMessage().trim();
		System.out.println("消息内容:" + msg);
		//判断name是否包含emoji表情
		//修复消息为表情加文字无法回复bug
		if (msg.contains("<face")) {
			do {
				String[] split1 = msg.split("<face");
				if(split1.length>1){
					int startIndex = msg.indexOf("<face");
					int endIndex = msg.indexOf("/>")+2;
					msg = msg.replace(msg.substring(startIndex, endIndex), "");
				}else{
					String[] split = msg.split("/>");
					Msg face = Msg.builder();
					Arrays.stream(split).forEach(s->{
						String number = Pattern.compile("[^0-9]").matcher(s).replaceAll("");
						face.face(Integer.parseInt(number));
					});
					helloPlugin.messagePrivateFacePush(bot,event,face);
					return MESSAGE_IGNORE;
				}
			}while (msg.contains("<face"));
		}else {
			if (msg.trim().contains("image")) {
				do {
					String[] split1 = msg.split("<image");
					if(split1.length>1){
						int startIndex = msg.indexOf("<image");
						int endIndex = msg.indexOf("/>")+2;
						msg = msg.replace(msg.substring(startIndex, endIndex), "");
					}else{
						String url = msg.substring(msg.indexOf("url=") + 5, msg.length() - 3);
						Msg image = Msg.builder().image(url);
						helloPlugin.messageImageFacePush(bot,event,image);
						return MESSAGE_IGNORE;
					}
				}while (msg.contains("<image"));
			}
		}

		String s = "";
		int i = 0;
		do {
			try {
				s = HttpRequestUtils.doGet("https://api.qingyunke.com/api.php?key=free&appid=0&msg=" + URLEncoder.encode(msg.trim(), "UTF-8"));
				i++;
			} catch (Exception e) {
				bot.sendPrivateMsg(event.getUserId(), Msg.builder().text(BannedConstant.WENTI), false);
			}
		} while (StringUtils.isBlank(s) && i < 10);
		JSONObject jsonObject = JSON.parseObject(s);
		if(jsonObject.getString("result").equals("0")){
			s=jsonObject.getString("content");
		}
		System.out.println("收到用户:" + event.getUserId() + "消息,内容为:" + msg);
		System.out.println(("给用户:" + event.getUserId() + "发送信息:" + s));
		helloPlugin.messagePrivatePush(s,bot,event);
		return MESSAGE_IGNORE;
	}
}
